package cn.anxingongyou.anxin.controller;

import cn.anxingongyou.anxin.entity.CommentWorker;
import cn.anxingongyou.anxin.service.CommentWorkerService;
import cn.anxingongyou.anxin.utils.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class CommentWorkerController {

    private final CommentWorkerService commentWorkerService;
    @Autowired
    public CommentWorkerController(CommentWorkerService commentWorkerService) {
        this.commentWorkerService = commentWorkerService;
    }
    ;

    @ApiOperation("评论内容是否安全")
    @ApiResponses({
            @ApiResponse(code = 201,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @RequestMapping(value = "/comment/worker/msgSecCheck",method = RequestMethod.POST)
    public Map msgSecCheck(
            @RequestBody @ApiParam(value = "信息", required = true) String content
    ){
        String res = commentWorkerService.getToken();
        boolean b = commentWorkerService.msgSecCheck(res, content);
        if(b){
            return Result.instance(201,"内容安全").toJsonMap();
        } else {
            return Result.instance(204,"内容不安全").toJsonMap();
        }
    }

    @ApiOperation("添加对工人评论信息")
    @ApiResponses({
            @ApiResponse(code = 201,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @RequestMapping(value = "/comment/worker/add",method = RequestMethod.POST)
    public Map addAdmin(
            @RequestBody @ApiParam(value = "评论信息", required = true) CommentWorker commentWorker
            ){
        commentWorker.setCreated(new Date());
        commentWorker.setUpdated(new Date());
        int res = commentWorkerService.insert(commentWorker);
        if(res > 0) {
            return Result.instance(201,"添加成功").toJsonMap();
        } else {
            return Result.instance(500,"添加失败").toJsonMap();
        }
    }

    @ApiOperation("api添加对工人评论信息")
    @ApiResponses({
            @ApiResponse(code = 201,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @RequestMapping(value = "/api/comment/worker/add",method = RequestMethod.POST)
    public Map apiAddCommentWorker(
            @RequestBody @ApiParam(value = "评论信息", required = true) CommentWorker commentWorker
    ){
        String token = commentWorkerService.getToken();
        boolean b = commentWorkerService.msgSecCheck(token, commentWorker.getContent());
        if(b){
            commentWorker.setCreated(new Date());
            commentWorker.setUpdated(new Date());
            int res = commentWorkerService.insert(commentWorker);
            if(res > 0) {
                return Result.instance(201,"添加成功").toJsonMap();
            } else {
                return Result.instance(500,"添加失败").toJsonMap();
            }
        }else{
            return Result.instance(204,"内容敏感！").toJsonMap();
        }

    }

    @ApiOperation("删除对工人评论信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "删除成功"),
            @ApiResponse(code = 500,message = "删除失败")
    })
    @RequestMapping(value = "/comment/worker/del/{id}",method = RequestMethod.GET)
    public Map delAdmin(
            @PathVariable @ApiParam(value = "评论主键", required = true) Long id
    ){
        int res = commentWorkerService.deleteByPrimaryKey(id);
        if(res > 0) {
            return Result.instance(204,"删除成功").toJsonMap();
        } else {
            return Result.instance(500,"删除失败").toJsonMap();
        }
    }

    @ApiOperation("更新对工人评论信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/comment/worker/update",method = RequestMethod.POST)
    public Map updateAdmin(
            @RequestBody @ApiParam(value = "工人评论信息,必传主键", required = true) CommentWorker commentWorker
    ){
        int res = commentWorkerService.updateByPrimaryKeySelective(commentWorker);
        if(res > 0) {
            return Result.instance(204,"更新成功").toJsonMap();
        } else {
            return Result.instance(500,"更新失败").toJsonMap();
        }
    }

    @ApiOperation("获取工人评论信息通过")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/comment/worker/findByComment",method = RequestMethod.POST)
    public Map findOneAdmin(
            @RequestBody @ApiParam(value = "工人评论信息", required = true) CommentWorker commentWorker
    ){
        CommentWorker adminl = commentWorkerService.selectOne(commentWorker);
        if(adminl != null) {
            return Result.<CommentWorker>instance(200,"获取成功",adminl).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取工人评论信息通过主键")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/comment/worker/findOneById/{id}",method = RequestMethod.GET)
    public Map findOneById(
            @PathVariable @ApiParam(value = "工人评论主键", required = true) Long id
    ){
        CommentWorker adminl = commentWorkerService.selectOneByPrimaryKey(id);
        if(adminl != null) {
            return Result.<CommentWorker>instance(200,"获取成功",adminl).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("api获取工人评论信息通过主键")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/api/comment/worker/findOneById/{id}",method = RequestMethod.GET)
    public Map apiFindOneById(
            @PathVariable @ApiParam(value = "工人评论主键", required = true) Long id
    ){
        CommentWorker adminl = commentWorkerService.selectOneByPrimaryKey(id);
        if(adminl != null) {
            return Result.<CommentWorker>instance(200,"获取成功",adminl).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取所有工人评论")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/comment/worker/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map selectAddressByAdmin(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "工人评论信息", required = true)  CommentWorker commentWorker){
        PageInfo<CommentWorker> pageInfo = commentWorkerService.page(pageNum, pageSize, commentWorker);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

    @ApiOperation("筛选工人评论")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/comment/worker/search/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map selectCommentByAdmin(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "工人评论信息", required = true)  CommentWorker commentWorker){
        PageInfo<CommentWorker> pageInfo = commentWorkerService.search(pageNum, pageSize, commentWorker);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

    @ApiOperation("api获取工人评论")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/api/comment/worker/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map apiSelectCommentWorkerByAdmin(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "工人评论信息", required = true)  CommentWorker commentWorker){
        PageInfo<CommentWorker> pageInfo = commentWorkerService.page(pageNum, pageSize, commentWorker);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }
}
